#!/usr/bin/python3.8
# -*- coding: utf-8 -*-
# @Time    : 2021/3/6 7:25 下午 
# @Author  : zhaohexin
# @Email   : 1483643520@xxx.com
# @File    : crud.py
# @Software: PyCharm 
# @FileDescription：部门具体逻辑

from sqlalchemy.orm import Session

from apps.system.depts import model
from apps.system.roles import model as roleModel


def get_dept(db: Session, dept_id: int):
    """
    获取单条部门数据
    :param db:
    :param dept_id:
    :return:
    """
    return db.query(model.Depts).filter(model.Depts.id == dept_id).first()


def get_depts(db: Session, pid: int = "", sort: str = "sort"):
    """
    获取所有部门（按照所转pid逐级查询）
    :param db:
    :param pid:
    :param sort:
    :return:
    """
    return db.query(model.Depts).filter(model.Depts.pid == pid).all()


def get_dept_by_role(db: Session, role_id: list):
    """
    根据所传角色列表返回部门列表
    :param db:
    :param role_id:
    :return:
    """
    return db.query(roleModel.RoleDepts).filter(roleModel.RoleDepts.role_id.in_(role_id)).all()
